﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 功能测试软件.BLL
{
    class TestViewBLL
    {
        private string iniPath;
        private View.TestView _testView = null;
        private BtLabelBLL.BtUartBLL bTBLL = null;

        public TestViewBLL(View.TestView testView)
        {
            iniPath = Environment.CurrentDirectory + "\\设置.ini";
            _testView = testView;
            bTBLL = new BtLabelBLL.BtUartBLL(_testView.BtPort, _testView.btLogView.RichTextBoxBt);
        }
        /// <summary>
        /// 读取配置文件
        /// </summary>
        /// <returns></returns>
        public Model.ConfigModel GetConfigModel()
        {
            try
            {
                Model.ConfigModel configModel = new Model.ConfigModel()
                {
                    BarDelay= BtLabelTool.IniHelper.IniReadValue("刷图", "图像数据发送等待", iniPath),

                    MultimeterUsbAddr= BtLabelTool.IniHelper.IniReadValue("电流", "万用表Addr", iniPath),
                    RelayPortname = BtLabelTool.IniHelper.IniReadValue("电流", "继电器串口", iniPath),
                    Current = BtLabelTool.IniHelper.IniReadValue("电流", "采集电流阈值", iniPath),
                    BtPortname = BtLabelTool.IniHelper.IniReadValue("信号", "蓝牙串口", iniPath),
                    ScanDelay = BtLabelTool.IniHelper.IniReadValue("信号", "蓝牙搜索时长", iniPath),
                    dBm = BtLabelTool.IniHelper.IniReadValue("信号", "蓝牙强度阈值", iniPath),

                    Channel1Enable = BtLabelTool.IniHelper.IniReadValue("通道1", "开启", iniPath),
                    Channel1Portname = BtLabelTool.IniHelper.IniReadValue("通道1", "串口", iniPath),
                    Channel1PicEnable = BtLabelTool.IniHelper.IniReadValue("通道1", "刷图", iniPath),
                    Channel1PicTemp = BtLabelTool.IniHelper.IniReadValue("通道1", "图片类型", iniPath),
                    Channel1RgbEnable = BtLabelTool.IniHelper.IniReadValue("通道1", "点灯", iniPath),

                    Channel2Enable = BtLabelTool.IniHelper.IniReadValue("通道2", "开启", iniPath),
                    Channel2Portname = BtLabelTool.IniHelper.IniReadValue("通道2", "串口", iniPath),
                    Channel2PicEnable = BtLabelTool.IniHelper.IniReadValue("通道2", "刷图", iniPath),
                    Channel2PicTemp = BtLabelTool.IniHelper.IniReadValue("通道2", "图片类型", iniPath),
                    Channel2RgbEnable = BtLabelTool.IniHelper.IniReadValue("通道2", "点灯", iniPath),

                    Channel3Enable = BtLabelTool.IniHelper.IniReadValue("通道3", "开启", iniPath),
                    Channel3Portname = BtLabelTool.IniHelper.IniReadValue("通道3", "串口", iniPath),
                    Channel3PicEnable = BtLabelTool.IniHelper.IniReadValue("通道3", "刷图", iniPath),
                    Channel3PicTemp = BtLabelTool.IniHelper.IniReadValue("通道3", "图片类型", iniPath),
                    Channel3RgbEnable = BtLabelTool.IniHelper.IniReadValue("通道3", "点灯", iniPath),

                    Channel4Enable = BtLabelTool.IniHelper.IniReadValue("通道4", "开启", iniPath),
                    Channel4Portname = BtLabelTool.IniHelper.IniReadValue("通道4", "串口", iniPath),
                    Channel4PicEnable = BtLabelTool.IniHelper.IniReadValue("通道4", "刷图", iniPath),
                    Channel4PicTemp = BtLabelTool.IniHelper.IniReadValue("通道4", "图片类型", iniPath),
                    Channel4RgbEnable = BtLabelTool.IniHelper.IniReadValue("通道4", "点灯", iniPath),

                    Channel5Enable = BtLabelTool.IniHelper.IniReadValue("通道5", "开启", iniPath),
                    Channel5Portname = BtLabelTool.IniHelper.IniReadValue("通道5", "串口", iniPath),
                    Channel5PicEnable = BtLabelTool.IniHelper.IniReadValue("通道5", "刷图", iniPath),
                    Channel5PicTemp = BtLabelTool.IniHelper.IniReadValue("通道5", "图片类型", iniPath),
                    Channel5RgbEnable = BtLabelTool.IniHelper.IniReadValue("通道5", "点灯", iniPath),
                };
                return configModel;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 初始化蓝牙，继电器串口
        /// </summary>
        /// <param name="config"></param>
        public void InitPort(Model.ConfigModel config)
        {
            try
            {
                if (_testView.BtPort.IsOpen == true)
                {
                    _testView.BtPort.Close();
                }
                if (_testView.RelayPort.IsOpen == true)
                {
                    _testView.RelayPort.Close();
                }
                if (config.BtPortname != "")
                {
                    BtLabelTool.PortHelper.InitPort(_testView.BtPort, config.BtPortname, config.BtPortBaudrate);
                }
                if (config.RelayPortname != "")
                {
                    BtLabelTool.PortHelper.InitPort(_testView.RelayPort, config.RelayPortname, config.RelayPortBaudrate);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 初始化通道
        /// </summary>
        /// <param name="config"></param>
        public void InitChannel(Model.ConfigModel config)
        {
            try
            {
                //界面
                if (_testView.Grid1.Children.Count == 0)
                {
                    _testView.Grid1.Children.Add(_testView.singelTestView1);
                    _testView.Grid2.Children.Add(_testView.singelTestView2);
                    _testView.Grid3.Children.Add(_testView.singelTestView3);
                    _testView.Grid4.Children.Add(_testView.singelTestView4);
                    _testView.Grid5.Children.Add(_testView.singelTestView5);
                }
                //测试状态
                View.TestView.ChannelsStep = new List<Model.StepModel.Step>()
                { 
                    Model.StepModel.Step.Wait,
                    Model.StepModel.Step.Wait,
                    Model.StepModel.Step.Wait,
                    Model.StepModel.Step.Wait,
                    Model.StepModel.Step.Wait
                };
                //赋值并初始化通道
                for (int i = 1; i < 6; i++)
                {
                    Model.TestModel testModel = null;
                    switch (i)
                    {
                        case 1:
                            testModel = _testView.singelTestView1._testModel;
                            testModel.BarDelay = int.Parse(config.BarDelay);
                            testModel.dBmMin = int.Parse(config.dBm);
                            testModel.RelayPort = _testView.RelayPort;
                            testModel.CurrentMax = double.Parse(config.Current);
                            testModel.ChannelNo = i;
                            testModel.ChannelEnable = config.Channel1Enable == "true";
                            testModel.ChannelPortname = config.Channel1Portname;

                            testModel.ChannelPicEnable = config.Channel1PicEnable == "true";
                            testModel.ChannelPicTemp = config.Channel1PicTemp;
                            testModel.ChannelRgbEnable = config.Channel1RgbEnable == "true";
                            testModel.RichTextBoxLog = _testView.channelLogView.RichTextBox1;
                            _testView.singelTestView1.Init(testModel); 
                            break;
                        case 2:
                            testModel = _testView.singelTestView2._testModel;
                            testModel.BarDelay = int.Parse(config.BarDelay);
                            testModel.dBmMin = int.Parse(config.dBm);
                            testModel.RelayPort = _testView.RelayPort;
                            testModel.CurrentMax = double.Parse(config.Current);
                            testModel.ChannelNo = i;
                            testModel.ChannelEnable = config.Channel2Enable == "true";
                            testModel.ChannelPortname = config.Channel2Portname;
                            testModel.ChannelPicEnable = config.Channel2PicEnable == "true";
                            testModel.ChannelPicTemp = config.Channel2PicTemp;
                            testModel.ChannelRgbEnable = config.Channel2RgbEnable == "true";
                            testModel.RichTextBoxLog = _testView.channelLogView.RichTextBox2;
                            _testView.singelTestView2.Init(testModel); 
                            break;
                        case 3:
                            testModel = _testView.singelTestView3._testModel;
                            testModel.BarDelay = int.Parse(config.BarDelay);
                            testModel.dBmMin = int.Parse(config.dBm);
                            testModel.RelayPort = _testView.RelayPort;
                            testModel.CurrentMax = double.Parse(config.Current);
                            testModel.ChannelNo = i;
                            testModel.ChannelEnable = config.Channel3Enable == "true";
                            testModel.ChannelPortname = config.Channel3Portname;
                            testModel.ChannelPicEnable = config.Channel3PicEnable == "true";
                            testModel.ChannelPicTemp = config.Channel3PicTemp;
                            testModel.ChannelRgbEnable = config.Channel3RgbEnable == "true";
                            testModel.RichTextBoxLog = _testView.channelLogView.RichTextBox3;
                            _testView.singelTestView3.Init(testModel);
                            break;
                        case 4:
                            testModel = _testView.singelTestView4._testModel;
                            testModel.BarDelay = int.Parse(config.BarDelay);
                            testModel.dBmMin = int.Parse(config.dBm);
                            testModel.RelayPort = _testView.RelayPort;
                            testModel.CurrentMax = double.Parse(config.Current);
                            testModel.ChannelNo = i;
                            testModel.ChannelEnable = config.Channel4Enable == "true";
                            testModel.ChannelPortname = config.Channel4Portname;
                            testModel.ChannelPicEnable = config.Channel4PicEnable == "true";
                            testModel.ChannelPicTemp = config.Channel4PicTemp;
                            testModel.ChannelRgbEnable = config.Channel4RgbEnable == "true";
                            testModel.RichTextBoxLog = _testView.channelLogView.RichTextBox4;
                            _testView.singelTestView4.Init(testModel); 
                            break;
                        case 5:
                            testModel = _testView.singelTestView5._testModel;
                            testModel.BarDelay = int.Parse(config.BarDelay);
                            testModel.dBmMin = int.Parse(config.dBm);
                            testModel.RelayPort = _testView.RelayPort;
                            testModel.CurrentMax = double.Parse(config.Current);
                            testModel.ChannelNo = i;
                            testModel.ChannelEnable = config.Channel5Enable == "true";
                            testModel.ChannelPortname = config.Channel5Portname;
                            testModel.ChannelPicEnable = config.Channel5PicEnable == "true";
                            testModel.ChannelPicTemp = config.Channel5PicTemp;
                            testModel.ChannelRgbEnable = config.Channel5RgbEnable == "true";
                            testModel.RichTextBoxLog = _testView.channelLogView.RichTextBox5;
                            _testView.singelTestView5.Init(testModel);
                            break;
                        default:
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 打开蓝牙，继电器串口
        /// </summary>
        public void OpenPorts()
        {
            try
            {
                if (View.TestView.config.BtPortname != "" && _testView.BtPort.IsOpen == false)
                {
                    _testView.BtPort.Open();
                }
                if (View.TestView.config.RelayPortname != "" && _testView.RelayPort.IsOpen == false)
                {
                    _testView.RelayPort.Open();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 扫描蓝牙
        /// </summary>
        /// <returns></returns>
        public List<BtLabelModel.DeviceModel> GetMacs()
        {
            try
            {
                return bTBLL.GetBTDevices(byte.Parse(View.TestView.config.ScanDelay),(sbyte)int.Parse(View.TestView.config.dBm));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
